package com.banciyuan.bcywebview.base.view.scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class ScrollViewObserver extends ScrollView {

    private OnScrollChangedListener onScrollChangedListener;
    private OnDragListener mOnDragListener;

    public ScrollViewObserver(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ScrollViewObserver(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public ScrollViewObserver(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        mGestureDetector = new GestureDetector(context,
                new OnGestureListener() {

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        return false;
                    }

                    @Override
                    public void onShowPress(MotionEvent e) {

                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                            float distanceX, float distanceY) {
//						if (null != mOnDragListener) {
//							if (distanceY < 0) {
//								mOnDragListener.onDragDown();
//							}
//							if (distanceY > 0) {
//								mOnDragListener.onDragUp();
//							}
//						}
//						canAnimate = true;
                        onScrollChangedListener.onScroll(distanceX, distanceY);
                        return false;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {

                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2,
                                           float velocityX, float velocityY) {
                        return false;
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        return false;
                    }
                });
    }

    public void setOnScrollListener(
            OnScrollChangedListener onScrollChangedListener) {
        this.onScrollChangedListener = onScrollChangedListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldX, int oldY) {
        super.onScrollChanged(x, y, oldX, oldY);
        if (onScrollChangedListener != null) {
            onScrollChangedListener.onScrollChanged(x, y, oldX, oldY);
        }
    }

    public interface OnScrollChangedListener {
        void onScrollChanged(int x, int y, int oldxX, int oldY);

        void onScroll(float distanceX, float distanceY);
    }

    public boolean isAtBottom() {
        return getScrollY() == getChildAt(getChildCount() - 1).getBottom()
                + getPaddingBottom() - getHeight();
    }

    public boolean isAtTop() {
        return getScrollY() <= 0;
    }

    private boolean atTop = false;
    private boolean atBottom = false;
    private GestureDetector mGestureDetector;

    private int startY;
    private boolean canAnimate = true;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = (int) ev.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                moveY = (int) ev.getY();
                canAnimate = startY != 0;
                break;
        }
        mGestureDetector.onTouchEvent(ev);
        return super.onTouchEvent(ev);
    }

    public void setOnDragListener(OnDragListener mOnDragListener) {
        this.mOnDragListener = mOnDragListener;
    }

    public interface OnDragListener {
        void onDragDown();

        void onDragUp();

        void onDrag();
    }

    private int downX;
    private int downY;
    private int moveY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getRawX();
                downY = (int) ev.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                int moveX = (int) ev.getRawX();
                int moveY = (int) ev.getRawY();
                if (Math.abs(downX - moveX) > Math.abs(downY - moveY)) {
                    return false;
                }
                break;

            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
